package com.zhj.bz.datastructure.linkedlist;

import com.zhj.algorithm.sort.BubbleSort;

/**
 * @author zhj
 */
public class DoubleLinkedListDemo {

    public static void main(String[] args) {
        DoubleLinkedList list = new DoubleLinkedList();
        HeroNode songJiang = new HeroNode(1, "宋江", "及时雨");
        HeroNode luJunYi = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode wuYong = new HeroNode(3, "吴用", "智多星");
        HeroNode linChong = new HeroNode(4, "林冲", "豹子头");
        list.addByOrder(linChong);
        list.addByOrder(luJunYi);
        list.addByOrder(songJiang);
        list.addByOrder(wuYong);
        list.show();
        songJiang = new HeroNode(1, "宋江", "及时雨");
        luJunYi = new HeroNode(2, "卢俊义", "玉麒麟");
        wuYong = new HeroNode(3, "吴用", "智多星");
        linChong = new HeroNode(4, "林冲", "豹子头");
        list.add(songJiang);
        list.add(luJunYi);
        list.add(wuYong);
        list.add(linChong);
        list.show();
        list.delete(1);
        list.show();
        HeroNode gongSunSheng = new HeroNode(4, "公孙胜", "入云龙");
        list.update(gongSunSheng);
        list.show();
        list.delete(3);
        list.delete(4);
        list.delete(5);
        list.delete(1);
        list.show();
    }
}
class DoubleLinkedList{
    private HeroNode head = new HeroNode();

    /**
     * 添加节点到最后
     * @param heroNode
     */
    public void add(HeroNode heroNode){
        HeroNode temp = head;
        while (temp.getNext() != null) {
            temp = temp.getNext();
        }
        temp.setNext(heroNode);
        heroNode.setPre(temp);
    }

    /**
     * 按顺序添加
     * @param heroNode
     */
    public void addByOrder(HeroNode heroNode){
        HeroNode temp = head;
        boolean flag = true;
        while (temp.getNext() != null) {
            temp = temp.getNext();
            if (temp.getNo() == heroNode.getNo()) {
                System.out.println("已存在相同序号的数据，不可重复添加");
            }
            if (temp.getNo() > heroNode.getNo()) {
                flag = false;
                break;
            }
        }
        if (flag) {
            temp.setNext(heroNode);
            heroNode.setPre(temp);
            heroNode.setNext(null);
            return;
        } else {
            heroNode.setPre(temp.getPre());
            heroNode.getPre().setNext(heroNode);
            temp.setPre(heroNode);
            heroNode.setNext(temp);
        }
    }

    /**
     * 修改
     * @param heroNode
     */
    public void update(HeroNode heroNode){
        if (head.getNext() == null) {
            System.out.println("链表为空,不能删除");
        }
        HeroNode temp = head;
        while (temp.getNext() != null) {
            temp = temp.getNext();
            if (temp.getNo() == heroNode.getNo()) {
                temp.getPre().setNext(heroNode);
                if (temp.getNext() != null) {
                    temp.getNext().setPre(heroNode);
                }
                return;
            }
        }
        System.out.println("没有找到对应序号");
    }

    /**
     * 删除
     * @param no
     */
    public void delete(int no){
        if (head.getNext() == null) {
            System.out.println("链表为空,不能删除");
        }
        HeroNode temp = head;
        while (temp.getNext() != null) {
            temp = temp.getNext();
            if (temp.getNo() == no) {
                temp.getPre().setNext(temp.getNext());
                if (temp.getNext() != null) {
                    temp.getNext().setPre(temp.getPre());
                }
                return;
            }
        }
        System.out.println("没有找到对应序号");
    }

    /**
     * 展示
     */
    public void show(){
        if (head.getNext() == null) {
            System.out.println("链表为空");
        }
        HeroNode temp = head;
        while (temp.getNext() != null) {
            temp = temp.getNext();
            System.out.println(temp);
        }
    }
}
class HeroNode {
    private int no;
    private String name;
    private String nickName;
    private HeroNode pre;
    private HeroNode next;

    public HeroNode(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    public HeroNode() {
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public HeroNode getPre() {
        return pre;
    }

    public void setPre(HeroNode pre) {
        this.pre = pre;
    }

    public HeroNode getNext() {
        return next;
    }

    public void setNext(HeroNode next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                "}";
    }
}